package cn.globalph.housekeeper.ui.task.check

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.findNavController
import cn.globalph.housekeeper.EventObserver
import cn.globalph.housekeeper.data.retrofit.HttpManager
import cn.globalph.housekeeper.databinding.FragmentCheckCardBinding
import cn.globalph.housekeeper.ui.BaseFragment
import kotlinx.android.synthetic.main.tool_bar.*

class CheckCardFragment :BaseFragment(){
    private lateinit var binding:FragmentCheckCardBinding
    @Suppress("UNCHECKED_CAST")
    private val viewModel by lazy {
        ViewModelProviders.of(this,object : ViewModelProvider.Factory{
            override fun <T : ViewModel?> create(modelClass: Class<T>): T {
                val repository = CheckCardRepository(HttpManager.getInstance())
                return CheckCardViewModel(repository) as T
            }
        }).get(CheckCardViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentCheckCardBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        binding.lifecycleOwner = this.viewLifecycleOwner
        binding.viewmodel = viewModel
        bar_title.text = "检查卡"
        bar_back.setOnClickListener{
            findNavController().popBackStack()
        }

        arguments?.apply {
            viewModel.taskId = getString("id")
            viewModel.attentions = getString("attentions")
        }

        binding.updateTv.setOnClickListener {
            viewModel.uploadRoomInfo(binding.bedroomLayout.number,
                binding.sittingRoomLayout.number,binding.kitchenLayout.number,
                binding.bashRoomLayout.number,binding.balconyLayout.number)
        }

        viewModel.toastEvent.observe(this,EventObserver{
            TT(it)
        })

        viewModel.roomInfo.observe(this, Observer {
            binding.bedroomLayout.number = it.room
            binding.sittingRoomLayout.number = it.hall
            binding.kitchenLayout.number = it.kitchen
            binding.bashRoomLayout.number = it.toilet
            binding.balconyLayout.number = it.balcony
        })
        viewModel.start()
    }
}